var _openId;
var _shopId;
var _orders = null;

var Order = function(orderId, money, integral, status) {
	this.orderId = orderId;
	this.money = money;
	this.integral = integral;
	this.status = status;
};

$(function() {
	_shopId = qmsk.getQueryValue("shopId");
	_openId = qmsk.getQueryValue("openId");
	$("#carJp").hide();
	loadOrderList();
	InitCount();
});

function loadOrderList() {
	qmsk.get('../order/detail/list/' + _openId, printOrderToPage);
}

//将订单信息显示到页面
function printOrderToPage(response) {
	$("#goods").empty();
	_orders = [];
	//订单 //{id,money}
	var row = response.row;
	if (row == 0) {
		$("#carJp").show();
		return;
	}

	$("#carJp").hide();
	//查询订单数量，并且创建外层DIV
	for (var i = 0; i < row; i++) {

		var j = 0;
		var orderId = response.getValue("orderId", i);
		var money = response.getValue("money", i);
		var integral = response.getValue("integral", i);
		var status = response.getValue("status", i);
		// 搜索该订单是否已经显示到页面上
		for (; j < _orders.length; j++) {
			if (orderId == _orders[j].orderId) {
				break;
			}
		}
		if (j == _orders.length) {

			var order = new Order(orderId, money, integral, status);
			_orders.push(order);

			var orderNo = response.getValue("orderNo", i);
			var status = response.getValue("status", i);
			status = getStatusName(status);
			var createFileTime = response.getValue("createFileTime", i);
			var receiver = response.getValue("revceive", i);
			var mp = response.getValue("mp", i);
			var fullLocation = response.getValue("fullLocation", i);
			var detailAddress = response.getValue("detailAddress", i);
			var $html = "<div id='" + orderId + "_order' class='order'>" + "<div class='time' style='padding:5px 10px;line-height:20px;font-size:12px;background:#f2f2f2;margin-bottom:10px;'>" + "订单编号：<span>" + orderNo + "</span>订单状态：" + status + "<br />" + "成交时间：<span>" + createFileTime + "</span><br>收货人：<span>" + receiver + "</span>&nbsp;&nbsp;&nbsp;&nbsp;<span>" + "手机号：" + mp + " </span><br><div style='height:28px;' >" + "收货地址：" + fullLocation + ' - ' + detailAddress + "</div></div>";
			$("#goods").append($html);
		}

		var GoodsName = response.getValue("GoodsName", i);
		var imageUrl = response.getValue("ImageUrl", i);
		var price = response.getValue("price", i);
		var saleIntegral = response.getValue("saleIntegral", i);
		var subTotal = response.getValue("subTotal", i);
		var subTtalIntegral = response.getValue("subTtalIntegral", i);
		var mode = response.getValue("Mode", i);
		var num = response.getValue("num", i);
		var orderNo = response.getValue("orderNo", i);

		$html = "<div class='product'>" + "<h2>" + GoodsName + "</h2>" + "<dl><dt><img src='" + imageUrl + "' /></dt>" + "<dd><p>规格： " + mode + "</p><p>兑换积分：<span class='dolor'>" + saleIntegral + "</span></p></dd></dl>" + "<div class='B_det'><span>数量：" + num + "</span><b>小计：" + subTtalIntegral + "</b></div></div>";
		$("#" + orderId + "_order").append($html);

	}
	//在每一个订单末尾追加取消和付款
	for (var i = 0; i < _orders.length; i++) {
		var order = _orders[i];
		var status = order.status;
		if (status != 0) {
			$html = "<div class='go_buy'>" + "<strong style='margin:0;'>总计：<b>" + order.integral + "</b></span></strong></div>" + "<div style='text-align:right;margin:10px;'></div></div>";
		} else {
			$html = "<div class='go_buy'>" + "<strong style='margin:0;'>总计：<b>" + order.integral + "</b></span></strong></div>" + "<div style='text-align:right;margin:10px;'>" + "<input id='delbt' style='float:left;height:22px;width:66px;' type='button' value='取消订单' onclick='delOrderM(" + order.orderId + ")' />" + "<a class='pay' onclick='payOrder(" + order.orderId + ")'>立即付款</a></div></div>";
		}
		$("#" + order.orderId + "_order").append($html);
	}
}

/**
 * 加载购物车 数量
 */
function InitCount() {
	qmsk.get('../cart/cartNums/' + _openId, function(response) {
		var count = response.getValue('count');
		if (count > 0) {
			$("#sup").text("(" + count + ")");
		} else {
			$("#sup").text('');
		}
	});
}

//取消订单
function delOrderM(orderId) {
	if (!confirm("确定要删除吗？")) {
		return;
	}
	qmsk.post('../order/del/' + orderId, function() {
		loadOrderList();
		alert("删除成功");
	});
}

//付款
function payOrder(orderId) {
	if (!confirm("确定要支付吗？")) {
		return;
	}
	qmsk.post('../order/pay/' + orderId + '/' + _openId + '/' + _shopId, function(response) {
		alert('支付成功！');
		loadOrderList();
	});
}

function getStatusName(status) {
	switch (status) {
		case 0:
			return "未兑换";
		case 1:
			return "已支付";
	}
}

/**
 * 到购物车页面
 */
function toBuyCar() {
	qmsk.redirectUrl('buy_car.html', {
		shopId : _shopId,
		openId : _openId
	});
}

/**
 * 刷新
 */
function myrefresh() {
	window.location.reload();
}

/**
 * 商品列表页面
 */
function toOrderForm() {
	qmsk.redirectUrl('order_form.html', {
		shopId : _shopId,
		openId : _openId
	});
}

/**
 * 商品列表页面
 */
function toProducts() {
	qmsk.redirectUrl('products.html', {
		shopId : _shopId,
		openId : _openId
	});
}
